package gw.com.android.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.gwtsz.gts2.hx.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gw.com.android.app.AppMain;
import www.com.library.view.BtnClickListener;
import www.com.library.view.TintTextView;

/**
 * 开关切换
 * @author reeta
 *
 */
public class CheckBoxView extends LinearLayout {

	@BindView(R.id.btn_left)
	TintTextView mLeftBtn;
	@BindView(R.id.btn_right)
	TintTextView mRightBtn;
	private BtnClickListener mListener;
	private Context cxt;

	public CheckBoxView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public CheckBoxView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public CheckBoxView(Context context) {
		super(context);
		init(context);
	}

	/**
	 * 初始化控件信息
	 * @param ctx
	 */
	private void init(Context ctx) {
		setOrientation(LinearLayout.HORIZONTAL);
		cxt = ctx;
		View view = LayoutInflater.from(ctx).inflate(R.layout.commom_check_view, this, true);
		ButterKnife.bind(this, view);
		mListener = null;
	}

	/**
	 * 给右边按钮设置文案
	 * @param titleID
	 */
	public void setRightResource(int titleID) {
		if (titleID == 0) {
			mRightBtn.setVisibility(View.GONE);
		} else {
			mRightBtn.setText(AppMain.getAppString(titleID));
			mRightBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 给左边按钮设置文案
	 * @param titleID
	 */
	public void setLeftResource(int titleID) {
		if (0 == titleID) {
			mLeftBtn.setVisibility(View.GONE);
		} else {
			mLeftBtn.setText(AppMain.getAppString(titleID));
			mLeftBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 给右边按钮设置文案
	 * @param title
	 */
	public void setRightResource(String title) {
		if (null == title || title.equals("")) {
			mRightBtn.setVisibility(View.GONE);
		} else {
			mRightBtn.setText(title);
			mRightBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 给左边按钮设置文案
	 * @param title
	 */
	public void setLeftResource(String title) {
		if (null == title || title.equals("")) {
			mLeftBtn.setVisibility(View.GONE);
		} else {
			mLeftBtn.setText(title);
			mLeftBtn.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 设置左右按钮的点击监听事件
	 * @param l
	 */
	public void setBtnClickListener(BtnClickListener l) {
		mListener = l;
	}

	@Override
	public void setFocusable(boolean focusable) {
		mLeftBtn.setFocusable(focusable);
		mRightBtn.setFocusable(focusable);
		super.setFocusable(focusable);
	}

	/**
	 * 改变左右按钮的状态，主要用于改变按钮的背景
	 * @param isRight
     */
	public void setChecked(boolean isRight) {
		if(isRight){
			mRightBtn.setSelected(true);
			mLeftBtn.setSelected(false);
		}else{
			mLeftBtn.setSelected(true);
			mRightBtn.setSelected(false);
		}
	}

	@OnClick(R.id.btn_left)
	void onLeftClick(){
		if (!mLeftBtn.isSelected()) {
			setChecked(false);
			if(mListener != null){
				mListener.onBtnClick(R.id.btn_left);
			}
		}
	}

	@OnClick(R.id.btn_right)
	void onRightClick(){
		if (!mRightBtn.isSelected()) {
			setChecked(true);
			if (mListener != null) {
				mListener.onBtnClick(R.id.btn_right);
			}
		}
	}
}